package com.xuetang9.fujian.mychat.biz;

import com.xuetang9.fujian.mychat.entity.BizType;
import com.xuetang9.fujian.mychat.entity.Message;
import com.xuetang9.fujian.mychat.entity.User;
import com.xuetang9.fujian.mychat.net.Server;
import com.xuetang9.fujian.mychat.net.SocketHandler;
import com.xuetang9.fujian.mychat.util.SysDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: fujian
 * @description:
 * @date: 2021/5/10 16:28
 * @version: V1.0
 * @package: com.xuetang9.fujian.mychat
 **/
public class ServerBiz {
    public SysDTO dealWithDTO(SysDTO dto){

        switch (dto.getBizType()){
            case 登录:
                return doLogin(dto);
            case 获得好友:
                return doGetOnlineUsers(dto);
            case 发送消息:
                return doSendMessage(dto);
            default:
                break;
        }

        return null;
    }

    private SysDTO doSendMessage(SysDTO dto) {
        Message message = dto.getMessage();
        String toUserNickName = message.getToUserNickName();
        dto.setBizType(BizType.您有新消息了);
        Server.SendDTO(toUserNickName,dto);
        return dto;
    }

    private SysDTO doGetOnlineUsers(SysDTO dto) {
        List<SocketHandler> handlerList = Server.GetOnLineHandler();
        List<User> userList = new ArrayList<>();
        for (SocketHandler handler : handlerList) {
            userList.add(handler.getCurrentUser());
        }
        dto.setBizType(BizType.获得好友成功);
        dto.setUserList(userList);
        return dto;
    }


    private SysDTO doLogin(SysDTO dto) {
        dto.setBizType(BizType.登录成功);
        User loginUser = new User(dto.getNickName());
        loginUser.setPassword(dto.getPassword());
        dto.setUser(loginUser);
        return dto;
    }
}
